package com.ligong.mapper;

import com.ligong.domain.Page;
import com.ligong.domain.Users;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class UserMapperTest {

    public static void main(String[] args) {
        //测试mapper代理开发方式可不可行

        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession=sqlSessionFactory.openSession();  //Connection


        //想象一下：mapper由谁调用：  service
        /*
        public class UserService{
          private UserMapper userMapper;

        main{
                userMapper.select();

        }

        }

         */
        //使用代理模式 生成接口的实现类
        UserMapper userMapper=sqlSession.getMapper(com.ligong.mapper.UserMapper.class);

        try {
//            Users user=userMapper.selectOneById(1);
//            System.out.println(user.getUserName());

            //测试插入
//            Users user=new Users();
//            user.setPassword("666666");
//            user.setUserName("普京99999999");
//            user.setUserSex(1);
//            int rows=userMapper.insertUser(user);
//            System.out.println(rows);
//            System.out.println(user.getUserId());

            //有时需要同时保存订单和订单明细，那么先保存订单，就需要获取到订单的号码，方便保存订单明细的时候用

            //测试更新
//            Users user=new Users();
//            user.setUserSex(0);
//            user.setUserName("baideng");
//            user.setPassword("888888");
//            user.setUserId(2916);

            //userMapper.updateUser(user);

            //测试删除
            //userMapper.deleteUser(2916);

            //测试模糊查询
//            String name="邯";
//            List<Users> usersList=userMapper.selectByLike("%"+name+"%");

            //测试分页
            //加入service传过来两个参数
            int pageNum=4;
            int pageSize=5;
            Page page=new Page();
            page.setStartIndex((pageNum-1)*pageSize);
            page.setPageSize(pageSize);
            List<Users> usersList=userMapper.selectByPageSelf(page);

            //sqlSession.commit();
            System.out.println(usersList.toString());

            sqlSession.close();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
